import 'package:check_app/dao/task_dao.dart';
import 'package:check_app/model/taskInfo.dart';
import 'package:check_app/page/SubjectDetailPage.dart';
import 'package:check_app/page/TempInputPage.dart';
import 'package:check_app/util/pref_util.dart';
import 'package:flutter/material.dart';
import 'package:check_app/widgets/PhoneSignal.dart';
import 'package:check_app/widgets/TempToolsList.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:convert' as convert;

import 'TaskDetailPage.dart';

class TempTaskPage extends StatefulWidget {
  final int id;
  final String code;
  const TempTaskPage({Key key, this.id, this.code}) : super(key: key);
  @override
  State<StatefulWidget> createState() => new _TempTaskPageState();
}

class _TempTaskPageState extends State<TempTaskPage> {
  bool _loading = false;
  int id;
  int customerId;
  int guideBookId;
  String type;
  String code;
  String status = 'FINISH';
  String customerName;
  String guideBookName;
  String contractNo;
  String provinceName;
  String provinceCode;
  String cityName;
  String cityCode;
  String countyName;
  String countyCode;
  int checkQuantity;
  List operatorList;
  List proofUrlList;
  List toolsList = [];

  @override
  void initState() {
    super.initState();
    id = widget.id;
    getData();
  }

  Future<Null> getData() async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await TaskDao.fetchTaskInfo('/task/' + id.toString());
      setState(() {
        _loading = false;
      });
      if (result.success) {
        setState(() {
          id = result.data.id;
          type = result.data.type;
          code = result.data.code;
          status = result.data.status;
          customerId = result.data.customerId;
          customerName = result.data.customerName;
          guideBookId = result.data.guideBookId;
          guideBookName = result.data.guideBookName;
          checkQuantity = result.data.checkQuantity;
          operatorList = result.data.operatorList;
          proofUrlList = result.data.proofUrlList;
          toolsList = result.data.subjectList;
          contractNo = result.data.contractNo;
          provinceName = result.data.provinceName;
          provinceCode = result.data.provinceCode;
          cityName = result.data.cityName;
          cityCode = result.data.cityCode;
          countyName = result.data.countyName;
          countyCode = result.data.countyCode;
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  Future<Null> submit() async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await TaskDao.submit({
        'id': id.toString(),
      });
      setState(() {
        _loading = false;
      });
      if (result.success) {
        Navigator.of(context).pop();
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(widget.code + '(临时)'),
        actions: <Widget>[
          GestureDetector(
            onTap: () async {
              final result = await Navigator.of(context)
                  .push(MaterialPageRoute(builder: (_) {
                return TaskDetailPage(
                  id: id,
                  type: type,
                  code: code,
                  contractNo: contractNo,
                  status: status,
                  customerId: customerId,
                  customerName: customerName,
                  guideBookId: guideBookId,
                  guideBookName: guideBookName,
                  checkQuantity: checkQuantity,
                  operatorList: operatorList,
                  proofUrlList: proofUrlList,
                  provinceName: provinceName,
                  provinceCode: provinceCode,
                  cityName: cityName,
                  cityCode: cityCode,
                  countyName: countyName,
                  countyCode: countyCode,
                );
              }));
              getData();
            },
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
              child: Text('详情'),
            ),
          ),
        ],
      ),
      body: SafeArea(
        child: Stack(
          children: <Widget>[
            TempToolsList(
              toolsList: toolsList,
              onclick: (item) async {
                if (status != 'FINISH') {
                  final result = await Navigator.of(context)
                      .push(MaterialPageRoute(builder: (_) {
                    return TempInputPage(taskId: id, code: code, id: item.id);
                  }));
                  print(result);
                  getData();
                } else {
                  Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                    return SubjectDetailPage(
                        id: item.id, subName: item.subName);
                  }));
                }
              },
            ),
            status != 'FINISH'
                ? Positioned(
                    right: 16,
                    bottom: 112,
                    child: GestureDetector(
                      onTap: () async {
                        final result = await Navigator.of(context)
                            .push(MaterialPageRoute(builder: (_) {
                          return TempInputPage(taskId: id, code: code);
                        }));
                        print(result);
                        getData();
                      },
                      child: Container(
                        width: 32,
                        height: 32,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(89, 131, 252, 1),
                          borderRadius: BorderRadius.circular(16),
                        ),
                        child: Image(
                          image: AssetImage('assets/images/add.png'),
                          width: 12,
                          height: 12,
                        ),
                      ),
                    ),
                  )
                : SizedBox(),
            status != 'FINISH'
                ? Positioned(
                    left: 0,
                    right: 0,
                    bottom: 24,
                    child: Container(
                      padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
                      width: double.infinity,
                      child: MaterialButton(
                        onPressed: toolsList.length < 1
                            ? null
                            : () async {
                                print('提交');
                                if (toolsList.length < 1) return null;
                                final result = await PrefUtil().openAlertDialog(
                                    context, '完成提交后，该任务不可新增待检物');
                                if (result == 'ok') {
                                  submit();
                                }
                              },
                        color: Colors.blue,
                        disabledColor: Colors.grey,
                        textColor: Colors.white,
                        height: 44,
                        child: Text(
                          '提交',
                          style: TextStyle(
                            fontSize: 14,
                            height: 24 / 17,
                          ),
                        ),
                      ),
                    ),
                  )
                : SizedBox(),
          ],
        ),
      ),
    );
  }
}
